package com.pxxy.mall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.pxxy.common.utils.R;
import com.pxxy.mall.ware.feign.ProductFeignApi;
import com.pxxy.common.to.SkuHasStockVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxxy.common.utils.PageUtils;
import com.pxxy.common.utils.Query;

import com.pxxy.mall.ware.dao.WareSkuDao;
import com.pxxy.mall.ware.entity.WareSkuEntity;
import com.pxxy.mall.ware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;


@Service("wareSkuService")
@AllArgsConstructor
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {


    private ProductFeignApi productFeignApi;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<WareSkuEntity>().lambda();

        String skuId = (String) params.get("skuId");

        if (!StringUtils.isEmpty(skuId)) {
            wrapper.eq(WareSkuEntity::getSkuId, skuId);
        }
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)) {
            wrapper.eq(WareSkuEntity::getWareId, wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params), wrapper

        );
        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        WareSkuEntity one = this.lambdaQuery().eq(WareSkuEntity::getSkuId, skuId).eq(WareSkuEntity::getWareId, wareId).one();
        WareSkuEntity wareSkuEntity = new WareSkuEntity();
        //如果还没有库存记录，就是新增
        if (Objects.isNull(one)) {
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            R r = productFeignApi.info(skuId);
            if (r.getCode() == 0) {
                Map<String, Object> map = (Map<String, Object>) r.get("skuInfo");
                wareSkuEntity.setSkuName((String) map.get("skuName"));
            }
            this.save(wareSkuEntity);
        } else {
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setStock(one.getStock() + skuNum);
            wareSkuEntity.setWareId(wareId);
            this.update(new UpdateWrapper<WareSkuEntity>().lambda()
                    .eq(WareSkuEntity::getSkuId, wareSkuEntity.getSkuId()).eq(WareSkuEntity::getWareId, wareSkuEntity.getWareId())
                    .set(WareSkuEntity::getStock, wareSkuEntity.getStock()));
        }

    }

    @Override
    public List<SkuHasStockVo> hasStock(List<Long> skuIds) {

        List<SkuHasStockVo> collect = skuIds.stream().map(t -> {
            SkuHasStockVo skuHasStockVo = new SkuHasStockVo();
            //查询当前sku的总库存
            Long count = baseMapper.getSkuStock(t);
            skuHasStockVo.setSkuId(t);
            skuHasStockVo.setHasStock(count==null?false:count > 0);
            return skuHasStockVo;
        }).collect(Collectors.toList());
        return collect;
    }


}